﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace TinyCinema.Bussiness.Menu
{
    public class Menus : System.Windows.DependencyObject
    {
        public static DependencyProperty CurrentViewProperty = DependencyProperty.Register("CurrentView", typeof(Data.Menu), typeof(Menus));
        public Data.Menu CurrentView
        {
            get { return (Data.Menu)GetValue(CurrentViewProperty); }
            set
            {
                SetValue(CurrentViewProperty, value);
                if ((value as Data.Menu).ID == null) IsEditable = true;
                else IsEditable = false;
            }
        }
        public static DependencyProperty IsDetailViewingProperty = DependencyProperty.Register("IsDetailViewing", typeof(bool), typeof(Menus));
        public bool IsDetailViewing
        {
            get { return (bool)GetValue(IsDetailViewingProperty); }
            set { SetValue(IsDetailViewingProperty, value); }
        }
        public static DependencyProperty lstMenuProperty = DependencyProperty.Register("lstMenu", typeof(ObservableCollection<Data.Menu>), typeof(Menus));
        public ObservableCollection<Data.Menu> lstMenu
        {
            get { return (ObservableCollection<Data.Menu>)GetValue(lstMenuProperty); }
            set { SetValue(lstMenuProperty, value); }
        }
        public static DependencyProperty IsEditableProperty = DependencyProperty.Register("IsEditable", typeof(bool), typeof(Menus));
        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set
            {
                SetValue(IsEditableProperty, value);
            }
        }
        public static DependencyProperty IsViewModeEnableProperty = DependencyProperty.Register("IsViewModeEnable", typeof(bool), typeof(Menus));
        public bool IsViewModeEnable
        {
            get { return (bool)GetValue(IsViewModeEnableProperty); }
            set
            {
                SetValue(IsViewModeEnableProperty, value);
            }
        }
        public static DependencyProperty IsSearchingProperty = DependencyProperty.Register("IsSearching", typeof(bool), typeof(Menus));
        public bool IsSearching
        {
            get { return (bool)GetValue(IsSearchingProperty); }
            set
            {
                SetValue(IsSearchingProperty, value);
            }
        }
        public static DependencyProperty strSearchProperty = DependencyProperty.Register("strSearch", typeof(string), typeof(Menus));
        public string strSearch
        {
            get { return (string)GetValue(strSearchProperty); }
            set
            {
                SetValue(strSearchProperty, value);
            }
        }

        public Messenger Messenger { get; set; }

        public ICommand AddCurrentViewCommand { get; set; }
        public ICommand CreateCommand { get; set; }
        public ICommand ChooseMenuCommand { get; set; }
        public ICommand EditCommand { get; set; }
        public ICommand BackCommand { get; set; }
        public ICommand SaveCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand AddPhotoCommand { get; set; }
        public ICommand SearchCommand { get; set; }
        public ICommand SearchCancelCommand { get; set; }
        public Menus()
        {
            Messenger = new Bussiness.Messenger();
            lstMenu = Data.Menu.GetAllMenu();
            SetValue(IsViewModeEnableProperty, true);            
            IsEditable = true;
            IsDetailViewing = false;
            ChooseMenuCommand = new RelayCommand<Data.Menu>(
                (s) => true,
                (s) =>
                {
                    CurrentView = s;
                    IsEditable = false;
                    IsViewModeEnable = false;
                    IsDetailViewing = true;
                }
            );
            CreateCommand = new RelayCommand<Data.Menu>(
                (s) => IsViewModeEnable,
                (s) =>
                {
                    CurrentView = new Data.Menu();
                    IsEditable = true;
                    IsViewModeEnable = false;
                    IsDetailViewing = true;

                }
            );
            EditCommand = new RelayCommand<Data.Menu>(
                (s) => !IsEditable,
                (s) =>
                {
                    IsEditable = true;
                }
            );
            SaveCommand = new RelayCommand<Data.Menu>(
                (s) => true,
                (s) =>
                {
                    if (CurrentView.ID == null) lstMenu.Add(CurrentView);
                    CurrentView.Save();
                    IsEditable = false;
                    IsDetailViewing = false;
                    IsViewModeEnable = true;
                }
            );
            DeleteCommand = new RelayCommand<Data.Menu>(
                (s) => !IsViewModeEnable||!IsEditable,
                (s) =>
                {
                    var message1 = new Message.DeleteConfirmation();
                    Messenger.Send(message1);
                    if (message1.Result==MessageBoxResult.Yes)
                        try
                        {
                            CurrentView.Delete();
                            lstMenu.Remove(CurrentView);
                            IsViewModeEnable = true;
                            IsEditable = true;
                            IsDetailViewing = false;
                        }
                        catch 
                        {
                            Messenger.Send(new Message.DeleteFailed());
                            Data.Execution.Restore();
                        }

                }
            );
            BackCommand = new RelayCommand<Data.Menu>(
                (s) => true,
                (s) =>
                {
                    if (IsEditable)
                    {
                        var message1 = new Message.BackConfimation();
                        Messenger.Send(message1);
                        if (message1.Result == MessageBoxResult.Yes)
                        {
                            Messenger.Send(new Message.BackConfimationOK());
                            IsViewModeEnable = true;
                            IsDetailViewing = false;
                            IsEditable = true; 
                        }
                    }
                    else
                    {
                        
                        Messenger.Send(new Message.BackConfimationOK());
                        IsViewModeEnable = true;
                        IsDetailViewing = false;
                        IsEditable = true; 
                    }
                    
                }
            );
            AddPhotoCommand = new RelayCommand<Object>(
                (s) => true,
                (s) =>
                {
                    CurrentView.AddPhoto();
                   
                }
            );
            SearchCommand = new RelayCommand<String>(
                (s) => true,
                (s) =>
                {
                    if (!IsSearching)
                    {
                        IsSearching = true;
                        return;
                    }
                    lstMenu = Data.Menu.SearchByName(s);
                 }
            );
            SearchCancelCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    IsSearching = false;
                    lstMenu = Data.Menu.GetAllMenu();
                }
            );
        }
        public static OrderReport LoadOrderReport()
        {
            string sql = "select CTHDTD.MaHDTD,TenTD,SoLuong,DonGia,(soluong*dongia) as ThanhTien, NgayHDTD, TenNV,TenKH from CTHDTD,THUCDON,HOADONTHUCDON,NHANVIEN,KHACHHANG where CTHDTD.MaHDTD = (select MAX(MaHDTD) from HOADONTHUCDON) and HOADONTHUCDON.MaHDTD = (select MAX(MaHDTD) from HOADONTHUCDON) and CTHDTD.MaTD = THUCDON.MaTD and NHANVIEN.MaNV = HOADONTHUCDON.MaNV and KHACHHANG.MaKH = HOADONTHUCDON.MaKH";
            OrderReport report = new OrderReport();
            report.SetDataSource(TinyCinema.Data.Execution.Excute(sql));
            return report;
        }
    }
}
